﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsBonusDAL.cs
/// 作  者：li/SAM
/// 时  间：2006.11.18/2007-01-08
/// 说  明：ClsBonusDAL类信息相关处理
/// </summary>
public class ClsBonusDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有的资料清单币别信息
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回资料清单信息数据集</returns>
    public DataSet GetBonus(string Co_ID, string User_ID, string Ds_ID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetBonus", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 搜索资料清单信息
    /// </summary>
    /// <param name="Key">搜索关键字</param>
    /// <returns>返回搜索信息数据集</returns>
    public DataSet SearchList(string Key)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchList", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@key", Key);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 作废资料清单信息
    /// </summary>
    /// <param>资料清单编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteBonus(string ID, int Is_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteBonus", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.NVarChar, 20);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@is_visible", SqlDbType.Int, 1);
        com.Parameters["@is_visible"].Value = Is_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认资料清单信息
    /// </summary>
    /// <param>资料清单编号</param>
    /// <param name="AuditStat">确认状态</param>
    /// <param name="Auditor">确认人</param>
    /// <returns>返回确认是否成功</returns>
    public Boolean AuditStatBonus(string ID, int AuditStat, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditStatSetBonus", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.NVarChar, 20);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@is_audit", SqlDbType.Int);
        com.Parameters["@is_audit"].Value = AuditStat;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加资料清单信息
    /// </summary>
    /// <param>资料清单编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    /// 
    public Boolean AddBonus(string ID, string Name, string Effect, string dept, string people, string bonusrule, decimal  bonusprice, decimal afterbonus,
        string item, string abstractt, int state, string Remark, string Oper)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddBonus", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.NVarChar, 20);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@name", SqlDbType.DateTime, 8);
        com.Parameters["@name"].Value = DateTime.Parse(Name); 
        try
        {
            com.Parameters.Add("@effect", SqlDbType.DateTime, 8);
            com.Parameters["@effect"].Value = DateTime.Parse(Effect);
        }
        catch
        {
            return false;
        }
        finally { }
            
        //中间插入各表的数据和参数

        com.Parameters.Add("@dept", SqlDbType.NVarChar, 60);
        com.Parameters["@dept"].Value = dept;
        com.Parameters.Add("@people", SqlDbType.NVarChar, 60);
        com.Parameters["@people"].Value = people;
        com.Parameters.Add("@bonusrule", SqlDbType.NVarChar, 60);
        com.Parameters["@bonusrule"].Value = bonusrule;
        com.Parameters.Add("@bonusprice", SqlDbType.Decimal, 40);
        com.Parameters["@bonusprice"].Value = bonusprice;
        com.Parameters.Add("@afterbonus", SqlDbType.Decimal, 40);
        com.Parameters["@afterbonus"].Value = afterbonus;
        com.Parameters.Add("@item", SqlDbType.NVarChar, 60);
        com.Parameters["@item"].Value = item;
        com.Parameters.Add("@abstractt", SqlDbType.NVarChar, 60);
        com.Parameters["@abstractt"].Value = abstractt;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 60);
        com.Parameters["@oper"].Value = Oper;
        com.Parameters.Add("@state", SqlDbType.Int, 1);
        com.Parameters["@state"].Value = state;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改资料清单信息
    /// </summary>
    /// <param>资料清单编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpDateBonus(string ID, string Name, string Effect, string dept, string people, string bonusrule,decimal afterbonus,
         string item, string abstractt, int state, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateBonus", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@id", SqlDbType.NVarChar, 20);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@name", SqlDbType.DateTime );
        com.Parameters["@name"].Value = DateTime.Parse(Name);
        com.Parameters.Add("@effect", SqlDbType.DateTime );
        com.Parameters["@effect"].Value =DateTime.Parse(Effect);                
        com.Parameters.Add("@dept", SqlDbType.NVarChar, 60);
        com.Parameters["@dept"].Value = dept;
        com.Parameters.Add("@people", SqlDbType.NVarChar, 60);
        com.Parameters["@people"].Value = people;
        com.Parameters.Add("@bonusrule", SqlDbType.NVarChar, 60);
        com.Parameters["@bonusrule"].Value = bonusrule;
        com.Parameters.Add("@afterbonus", SqlDbType.Decimal, 28);
        com.Parameters["@afterbonus"].Value = afterbonus;
        //com.Parameters.Add("@bonusprice", SqlDbType.Decimal, 28);
        //com.Parameters["@afterbonus"].Value = decimal.Parse("0");
        com.Parameters.Add("@item", SqlDbType.NVarChar, 60);
        com.Parameters["@item"].Value = item;
        com.Parameters.Add("@abstractt", SqlDbType.NVarChar, 60);
        com.Parameters["@abstractt"].Value = abstractt;
        com.Parameters.Add("@state", SqlDbType.Int, 10);
        com.Parameters["@state"].Value = state;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

}
